<template>
  <el-dialog :visible.sync="visible" v-bind="config">
    <slot v-if="visible"></slot>
    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitLoading"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      visible: false,
      config: {
        title: "提示",
      },
      ok: () => {
        this.resetForm();
        return this.$nextTick();
      },
      close: () => {
        return this.$nextTick();
      },
      submitLoading: false,
    };
  },
  methods: {
    open(val = {}) {
      const { ok, close, ...rest } = val;
      Object.assign(this.config, { ...rest });
      if (ok) {
        this.ok = ok;
      }
      if (close) {
        this.close = close;
      }
      this.visible = true;
    },
    resetForm() {
      this.close().finally(() => {
        this.visible = false;
      });
    },

    handleSubmit() {
      this.submitLoading = true;
      this.ok().finally(() => {
        this.submitLoading = false;
        this.resetForm();
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
